﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CodeCamp12.Domain;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Web.Script.Services;
using System.Web.Services;
using CodeCamp12.BLL;

namespace CodeCamp12
{
    /// <summary>
    /// Summary description for GetImageForSpeaker
    /// </summary>
    public class GetImageForSpeaker : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            var speakerId = HttpContext.Current.Request.QueryString["id"];

            try
            {
                if (string.IsNullOrEmpty(speakerId))
                {
                    throw new Exception("In GetImageForSpeaker.ashx : ProcessRequest - query string speakerId can not be null or empty");
                }
                else
                {
                    var speakerBuissProvide = new SpeakersBL();
                    var file = speakerBuissProvide.GetImageForSpeaker(new Guid(speakerId));

                    if (file.FileType.Equals(".jpg"))
                        context.Response.ContentType = "image/jpg";
                    else
                        context.Response.ContentType = "image/png";
                    var data = new byte[file.FileData.Length];
                    file.FileData.Read(data, 0, (int)file.FileData.Length);
                    file.FileData.Dispose();
                    context.Response.Clear();
                    context.Response.BinaryWrite(data);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}